// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var base = require("base");
	var databind = require("databind");
	var tools = require("tools");
	//	var _css = require("./login.css");

	var init = function() {
		testSummayry();
		initEvent();
	};
	var initEvent = function() {
//		showChart1();//前十货品
//		showChart2();//前十客户
		showChart3();//前十违约
		inquireRankingDatas(1, 1);
		inquireRankingDatas(2, 2);
		inquireEnterpriseFinanceSummaryPageList();
		inquireManagerBordDetail();
		
		//直客授信额度
		$(".enterpriseCreditLine").unbind("click").click(function(){
			window.open("../creditLine/creditLine.html?enterpriseType=0");
		});
		
		//分销商授信额度
		$(".distributorsCreditLine").unbind("click").click(function(){
			window.open("../creditLine/creditLine.html?enterpriseType=1");
		});
		
		//直客敞口额度
		$(".enterpriseCreditLineAlready").unbind("click").click(function(){
			window.open("../creditLine/creditLine.html?enterpriseType=0&isAlready=true");
		});
		
		//分销商敞口额度
		$(".distributorsCreditLineAlready").unbind("click").click(function(){
			window.open("../creditLine/creditLine.html?enterpriseType=1&isAlready=true");
		});
		
		//当日放款
		$(".todayPayment").unbind("click").click(function(){
			window.open("../receivePaymentDetail/receivePaymentDetail.html?type=7");
		});
		
		//当前放款
		$(".payment").unbind("click").click(function(){
			window.open("../receivePaymentDetail/receivePaymentDetail.html?type=1");
		});
		
		//累计放款
		$(".accumulatePayment").unbind("click").click(function(){
			window.open("../receivePaymentDetail/receivePaymentDetail.html?type=2");
		});
		
		//当日还款
		$(".todayReceive").unbind("click").click(function(){
			window.open("../receivePaymentDetail/receivePaymentDetail.html?type=6");
		});
		
		//当前还款
		$(".receive").unbind("click").click(function(){
			window.open("../receivePaymentDetail/receivePaymentDetail.html?type=5");
		});
		
		//累计还款
		$(".accumulateReceive").unbind("click").click(function(){
			window.open("../receivePaymentDetail/receivePaymentDetail.html?type=3");
		});
		
		//累计收息
		$(".interest").unbind("click").click(function(){
			window.open("../receivePaymentDetail/receivePaymentDetail.html?type=4");
		});
		
		//资金金额
		$(".loansDetail").unbind("click").click(function(){
			window.open("../loansDetail/loansDetail.html?type=4");
		});
	};

	var inquireRankingDatas = function(type, charNum) {
		base.fnAjax({
			action: "inquireRankingDatas.json",
			async: true,
			nocode: true,
			oData: {
				type: type
			}, //传递参数
			fnSuccess: function(data) {
				if(data.status == 200) {
//					data1 = data.datas;
					var d1 = [];
					$.each(data.datas, function() {
						var _this = this;
						d1.push(_this.name);
					});
					if (charNum == 1) {
						showChart1(data.datas.slice(0, 10), d1);
					} else if (charNum == 2) {
						showChart2(data.datas.slice(0, 10), d1);
					}
				} else { 
					layer.msg(data.message);
				}
			}
		});
	};
	
	var inquireManagerBordDetail = function() {
		base.fnAjax({
			action: "inquireManagerBordDetail.json",
			async: true,
			nocode: true,
			oData: {
			}, //传递参数
			fnSuccess: function(data) {
				if(data.status == 200) {
					data.datas[0].enterpriseCreditLineAlready = (data.datas[0].enterpriseCreditLineAlready/10000).toFixed(2);
					data.datas[0].distributorsCreditLineAlready = (data.datas[0].distributorsCreditLineAlready/10000).toFixed(2);
					data.datas[0].enterpriseCreditLine = (data.datas[0].enterpriseCreditLine/10000).toFixed(2);
					data.datas[0].distributorsCreditLine = (data.datas[0].distributorsCreditLine/10000).toFixed(2);
					data.datas[0].currentLoanAmount = (data.datas[0].currentLoanAmount/10000).toFixed(2);
					data.datas[0].loanAmount = (data.datas[0].loanAmount/10000).toFixed(2);
					data.datas[0].receiveAmount = (data.datas[0].receiveAmount/10000).toFixed(2);
					data.datas[0].capital = (data.datas[0].capital/10000).toFixed(2);
					data.datas[0].currentReceiveAmount = (data.datas[0].currentReceiveAmount/10000).toFixed(2);
					data.datas[0].interestAmount = (data.datas[0].interestAmount/10000).toFixed(2);
					data.datas[0].todayReceiveAmount = (data.datas[0].todayReceiveAmount/10000).toFixed(2);
					data.datas[0].todayLoanAmount = (data.datas[0].todayLoanAmount/10000).toFixed(2);
					databind.fillForm($(".managerBord"), data.datas[0]);
					
					$.each($(".data"), function(){
						var $this = $(this);
						$this.html(tools.formatCurrency($this.html()));
					});
				} else { 
					layer.msg(data.message);
				}
			}
		});
	};

	var inquireEnterpriseFinanceSummaryPageList = function() {
		//loading层
		base.fnAjax({
			action: "inquireEnterpriseFinanceSummaryPageList.json",
			async: true,
			nocode: true,
			oData: {
				pageSize: 1000
			}, //传递参数
			fnSuccess: function(data) {
				if(data.status == 200) {
					juicer.register("formatCurrency", tools.formatCurrency);
					var tpl = $(".summaryTpl").html();
					var html = juicer(tpl, data);
					$(".summaryTab tbody").html(html);
					$('.summaryTab').stickySort({ 
						sortable: true
					});
				} else { 
					layer.msg(data.message);
				}
			}
		});
	};
	
	var testSummayry = function() {
		base.fnAjax({
			action: "testSummayry.json",
			async: true,
			oData: {
				pageSize: 1000
			}, //传递参数
			fnSuccess: function(data) {
				if(data.status == 200) {
				} else { 
					layer.msg(data.message);
				}
			}
		});
	};
	
	var showChart1 = function(d, d1) {
		// 基于准备好的dom，初始化echarts实例
		var myChart1 = echarts.init(document.getElementById('main1'));
		// 指定图表的配置项和数据
		//app.title = '嵌套环形图';

		var option = {
			title: {
				text: '',
				subtext: '一润供应链',
				x: 'left'
			},
			tooltip: {
				trigger: 'item',
				formatter: "{a} <br/>{b} : {c} ({d}%)"
			},
			legend: {
				 orient: 'horizontal',
		         x: 'left',
		         y:'bottom',		
		        data:d1
		    },
			toolbox: {
				show: true,
				feature: {
					mark: {
						show: true
					},
					dataView: {
						show: true,
						readOnly: false
					},
					magicType: {
						show: true,
						type: ['pie', 'funnel']
					},
					restore: {
						show: true
					},
					saveAsImage: {
						show: true
					}
				}
			},
			calculable: true,
			series: [{
				name: '访问来源',
				type: 'pie',
				radius: ['30%', '50%'],
				avoidLabelOverlap: false,
				center: ['50%', '60%'],
				data: d,
				itemStyle: {
					emphasis: {
						shadowBlur: 10,
						shadowOffsetX: 0,
						shadowColor: 'rgba(0, 0, 0, 0.5)'
					}
				}
			}]
		};

		// 使用刚指定的配置项和数据显示图表。
		myChart1.setOption(option);
		// 处理点击事件并且跳转到相应的百度搜索页面
		myChart1.on('click', function(params) {
			//window.open('https://www.baidu.com/s?wd=' + encodeURIComponent(params.name));
			window.open('../rankingDetail/rankingDetail.html?goodsName=' + encodeURI(encodeURI(params.name)) + '&bulkstockInfoId=' + params.data.detailId);
		});
	};

	var showChart2 = function(d, d1) {
		// 基于准备好的dom，初始化echarts实例
		var myChart2 = echarts.init(document.getElementById('main0'));
		// 指定图表的配置项和数据
		//app.title = '嵌套环形图';

		var option = {
			title: {
				text: '',
				subtext: '一润供应链',
				x: 'left'
			},
			tooltip: {
				trigger: 'item',
				formatter: "{a} <br/>{b} : {c} ({d}%)"
			},

			toolbox: {
				show: true,
				feature: {
					mark: {
						show: true
					},
					dataView: {
						show: true,
						readOnly: false
					},
					magicType: {
						show: true,
						type: ['pie', 'funnel']
					},
					restore: {
						show: true
					},
					saveAsImage: {
						show: true
					}
				}
			},
			legend: {
				 orient: 'horizontal',
		         x: 'left',
		         y:'bottom',		
		        data:d1
		    },
			calculable: true,
			series: [{
				name: '融资金额',
				type: 'pie',
				radius: ['30%', '50%'],
				avoidLabelOverlap: false,
				center: ['50%', '60%'],
				data: d,
				itemStyle: {
					emphasis: {
						shadowBlur: 10,
						shadowOffsetX: 0,
						shadowColor: 'rgba(0, 0, 0, 0.5)'
					}
				}
			}]
		};

		// 使用刚指定的配置项和数据显示图表。
		myChart2.setOption(option);
		// 处理点击事件并且跳转到相应的百度搜索页面
		myChart2.on('click', function(params) {
			//window.open('https://www.baidu.com/s?wd=' + encodeURIComponent(params.name));
			window.open('../rankingDetail/rankingDetail1.html?customerName=' + encodeURI(encodeURI(params.name)) + '&enterpriseId=' + params.data.detailId);
		});
	};

	var showChart3 = function() {
		// 基于准备好的dom，初始化echarts实例
		var myChart2 = echarts.init(document.getElementById('main2'));

		// 指定图表的配置项和数据
		//app.title = '嵌套环形图';

		var option = {
			color: ['#3398DB'],
			title: {
				text: '客户违约排行',
				subtext: '一润供应链'
			},
			tooltip: {
				trigger: 'axis'
			},
			legend: {
				data: ['违约']
			},
			tooltip: {
				trigger: 'axis',
				axisPointer: { // 坐标轴指示器，坐标轴触发有效
					type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
				}
			},
			grid: {
				left: '3%',
				right: '4%',
				bottom: '3%',
				containLabel: true
			},
			toolbox: {
				show: true,
				feature: {
					dataView: {
						show: true,
						readOnly: false
					},
					magicType: {
						show: true,
						type: ['line', 'bar']
					},
					restore: {
						show: true
					},
					saveAsImage: {
						show: true
					}
				}
			},
			calculable: true,
			xAxis: [{
				type: 'category',
				data: ['浙江永福', '福建鑫泰', '江苏有限', '上海永新', '南京化工', '广州南方', '海南永康', '天津豪迈'],
				axisTick: {
					alignWithLabel: true
				}
			}],
			yAxis: [{
				type: 'value'
			}],
			series: [{
				name: '收款',
				type: 'bar',
				barWidth: '60%',
				label: {
					normal: {
						show: true,
						position: 'inside'
					}
				},
				data: [0, 0, 0, 0, ,0, 0, 0, 0]
			}]
		};

		// 使用刚指定的配置项和数据显示图表。
		myChart2.setOption(option);
	}

	return {
		init: init
	};
});